﻿using System;
using System.Web.Mvc;
using System.Reflection;
using ProjetoComperio.Framework.Util;
using ProjetoComperio.Framework.Web.Logging;
using ProjetoComperio.Framework.Web.Security;

namespace ProjetoComperio.SystemFramework.Web
{
    //[ExceptionLogging]
    public class ComperioController : System.Web.Mvc.Controller
    {



        /// <summary>
        /// This method fill a tracked'entity using the FormCollection data
        /// It should be override if the form works with some advanced control
        /// such as selects or dropdownlist
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="form"></param>
        public virtual void FillEntity(Object entity, FormCollection form)
        {
            foreach (String key in form.AllKeys)
            {
                PropertyInfo propertyInfo = entity.GetType().GetProperty(key);
                if (propertyInfo != null)
                    SetPropertyValue(propertyInfo, entity, form[key]);
            }
        }

        /// <summary>
        /// This method fill the viewdata with values of the entity's properties
        /// </summary>
        /// <param name="viewData"></param>
        /// <param name="entity"></param>
        public virtual void FillViewData(ViewDataDictionary viewData, Object entity)
        {

            foreach (var item in entity.GetType().GetProperties())
                viewData[item.Name] = item.GetValue(entity, null);

        }


        private void SetPropertyValue(PropertyInfo propertyInfo, Object entity, String value)
        {

            switch (propertyInfo.PropertyType.FullName)
            {
                case "System.String":
                    propertyInfo.SetValue(entity, value, null);
                    break;
                case "System.DateTime":
                    {
                        if (value.IsParseableToDateTime())
                            propertyInfo.SetValue(entity, Convert.ToDateTime(value), null);
                        else
                            propertyInfo.SetValue(entity, DateTime.MinValue, null);
                        break;
                    }
                case "System.Int32":
                    propertyInfo.SetValue(entity, Convert.ToInt32(value), null);
                    break;

                case "System.Boolean":
                    {
                        if (value.IsParseableToBoolean())
                        {
                            if (value.Equals("on"))
                                value = "True";
                            propertyInfo.SetValue(entity, Convert.ToBoolean(value), null);
                        }
                        else
                            propertyInfo.SetValue(entity, default(Boolean), null);
                        break;
                    }


            }

            //if (propertyInfo.PropertyType == System.DateTime.Now.GetType())
            //{
            //    if()

            //    propertyInfo.SetValue(entity, Convert.ToDateTime("10/03/2009"), null);
            //}
            //else if (propertyInfo.PropertyType == String.Empty.GetType())
            //    propertyInfo.SetValue(entity, value, null);
            //else if (propertyInfo.PropertyType == Int32.MinValue.GetType())
            //    propertyInfo.SetValue(entity, Convert.ToInt32(value), null);
            //else if (propertyInfo.PropertyType == Decimal.Zero.GetType())
            //    propertyInfo.SetValue(entity, Convert.ToDecimal(value), null);
            //else if (propertyInfo.PropertyType == Boolean.TrueString.GetType())
            //    propertyInfo.SetValue(entity, Convert.ToBoolean(value), null);
        }

        private User _currentUser;
        public User CurrentUser
        {
            get
            {
                if (_currentUser == null)
                    _currentUser = new ComperioMemberShipProvider().RetrieveActiveUser(HttpContext.User.Identity.Name);
                return _currentUser;
            }

        }

    }


}
